package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.item.ItemExpansionSourceDTO;
import com.etone.smartAudit.dto.task.*;
import com.etone.smartAudit.enums.CheckState;
import com.etone.smartAudit.enums.TaskType;
import com.etone.smartAudit.service.TaskAuditRecordService;
import com.etone.smartAudit.service.TaskExecService;
import com.etone.smartAudit.service.TaskHistoryService;
import com.etone.smartAudit.service.TaskService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.task.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-06-30 11:30:57
 * @Description TaskController
 */
@RestController
@AllArgsConstructor
@Api(tags = "任务管理", value = "TaskController")
public class TaskController {
    private TaskService taskService;

    private TaskHistoryService taskHistoryService;

    private TaskExecService taskExecService;

    private TaskAuditRecordService taskAuditRecordService;

    @ApiOperation(value = "任务列表《完》")
    @GetMapping("/tasks")
    public PagerVO<TaskListVO> list(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "任务名称") @RequestParam(required = false) String name,
            @ApiParam(value = "任务类型") @RequestParam(required = false) TaskType type,
            @ApiParam(value = "申请人名称") @RequestParam(required = false) String createAdminName,
            @ApiParam(value = "执行人名称") @RequestParam(required = false) String execAdminName,
            @ApiParam(value = "申请时间-开始时间") @RequestParam(required = false) Date createStartTime,
            @ApiParam(value = "申请时间-结束时间") @RequestParam(required = false) Date createEndTime,
            @ApiParam(value = "审核状态") @RequestParam(required = false) CheckState state,
            @ApiParam(value = "是否完结") @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "申请人id") @RequestParam(required = false) Integer createAdminId,
            @ApiParam(value = "是否延期") @RequestParam(required = false) Boolean isDelay

    ) {
        return taskService.list(name, type, createAdminName, execAdminName, createStartTime, createEndTime, state, isOver, itemId, createAdminId, admin, isDelay, page, pageSize);
    }

    @ApiOperation(value = "任务导出《完》")
    @GetMapping("/tasks/export")
    public void export(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "任务名称") @RequestParam(required = false) String name,
            @ApiParam(value = "任务类型") @RequestParam(required = false) TaskType type,
            @ApiParam(value = "申请人名称") @RequestParam(required = false) String createAdminName,
            @ApiParam(value = "执行人名称") @RequestParam(required = false) String execAdminName,
            @ApiParam(value = "申请时间-开始时间") @RequestParam(required = false) Date createStartTime,
            @ApiParam(value = "申请时间-结束时间") @RequestParam(required = false) Date createEndTime,
            @ApiParam(value = "审核状态") @RequestParam(required = false) CheckState state,
            @ApiParam(value = "是否完结") @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "申请人id") @RequestParam(required = false) Integer createAdminId,
            @ApiParam(value = "是否逾期") @RequestParam(required = false) Boolean isDelay,
            HttpServletResponse response
    ) {
        taskService.export(name, type, createAdminName, execAdminName, createStartTime, createEndTime, state, isOver, itemId, createAdminId, admin, isDelay, response);
    }

    @ApiOperation(value = "任务统计《完》")
    @GetMapping("/tasks/analysis")
    public TaskAnalysisTotal analysisTotal(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "任务名称") @RequestParam(required = false) String name,
            @ApiParam(value = "任务类型") @RequestParam(required = false) TaskType type,
            @ApiParam(value = "申请人名称") @RequestParam(required = false) String createAdminName,
            @ApiParam(value = "执行人名称") @RequestParam(required = false) String execAdminName,
            @ApiParam(value = "申请时间-开始时间") @RequestParam(required = false) Date createStartTime,
            @ApiParam(value = "申请时间-结束时间") @RequestParam(required = false) Date createEndTime,
            @ApiParam(value = "审核状态") @RequestParam(required = false) CheckState state,
            @ApiParam(value = "是否完结") @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "申请人id") @RequestParam(required = false) Integer createAdminId
    ) {
        return taskService.TaskAnalysisTotal(name, type, createAdminName, execAdminName, createStartTime, createEndTime, state, isOver, itemId, createAdminId, admin);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "增加模型任务《完》")
    @PostMapping("/tasks/types/MODULE")
    public void addModuleTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated ModuleTaskDTO moduleTaskDTO
    ) {
        taskService.add(moduleTaskDTO, admin, TaskType.MODULE);
    }

    @AdminOperation(module = "任务操作", item = "添加工作任务")
    @ApiOperation(value = "增加工作任务《完》")
    @PostMapping("/tasks/types/WORK")
    public void addWorkTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated WorkTaskDTO workTaskDTO
    ) {
        taskService.addWorkTask(workTaskDTO, admin);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "增加临时资源申请《完》")
    @PostMapping("/tasks/types/TEMPORARY")
    public void addTemporaryTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated TemporaryTaskDTO temporaryTaskDTO
    ) {
        taskService.add(temporaryTaskDTO, admin, TaskType.TEMPORARY);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑模型任务《完》")
    @PutMapping("/tasks/{id}/types/MODULE")
    public void editModuleTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated ModuleTaskDTO moduleTaskDTO
    ) {
        taskService.edit(id, moduleTaskDTO, admin, TaskType.MODULE);
    }

    @AdminOperation(module = "任务操作", item = "添加工作任务")
    @ApiOperation(value = "编辑工作任务《完》")
    @PutMapping("/tasks/{id}/types/WORK")
    public void editWorkTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "工作任务表单") @RequestBody @Validated WorkTaskDTO workTaskDTO
    ) {
        taskService.editWorkTask(id, workTaskDTO, admin);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑临时任务《完》")
    @PutMapping("/tasks/{id}/types/TEMPORARY")
    public void addTemporaryTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "临时任务表单") @RequestBody @Validated TemporaryTaskDTO temporaryTaskDTO
    ) {
        taskService.edit(id, temporaryTaskDTO, admin, TaskType.TEMPORARY);
    }

    @AdminOperation(module = "任务操作", item = "审核")
    @ApiOperation(value = "任务审批《完》")
    @PostMapping("/tasks/audit/{id}")
    public void audit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "审批内容") @RequestBody @Validated TaskAuditDTO taskAuditDTO
    ) {
        taskService.audit(id, taskAuditDTO, admin);
    }

    @ApiOperation(value = "服务支撑中台任务详情《完》")
    @GetMapping("/tasks/{id}/types/WORK_SERVICE")
    public ServiceExpertWorkTaskVO workServiceTaskDetail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskService.workServiceTaskDetail(id);
    }

    @ApiOperation(value = "模型任务详情《完》")
    @GetMapping("/tasks/{id}/types/MODULE")
    public ModuleTaskDetailVO moduleTaskDetail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskService.moduleTaskDetail(id);
    }

    @ApiOperation(value = "工作任务详情《完》")
    @GetMapping("/tasks/{id}/types/WORK")
    public WorkTaskDetailVO workTaskDetail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskService.workTaskDetail(id);
    }

    @ApiOperation(value = "临时任务详情《完》")
    @GetMapping("/tasks/{id}/types/TEMPORARY")
    public TemporaryTaskDetailVO temporaryTaskDetail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskService.temporaryTaskDetail(id);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "增加数据模型授权任务《完》")
    @PostMapping("/tasks/types/DATA_MODEL_AUTH")
    public void addDataModelAuthTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated DataModelAuthTaskDTO dataModelAuthTaskDTO
    ) {
        taskService.add(dataModelAuthTaskDTO, admin, TaskType.DATA_MODEL_AUTH);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑数据模型授权任务《完》")
    @PutMapping("/tasks/{id}/types/DATA_MODEL_AUTH")
    public void editDataModelAuthTask(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated DataModelAuthTaskDTO dataModelAuthTaskDTO
    ) {
        taskService.edit(id, dataModelAuthTaskDTO, admin, TaskType.DATA_MODEL_AUTH);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "添加资源申请《完》")
    @PostMapping("/tasks/types/RESOURCE_APPLY")
    public void addFirstExpansion(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "资源申请表单") @RequestBody ResourceTaskDTO resourceTaskDTO
    ) {
        taskService.add(resourceTaskDTO, admin, TaskType.RESOURCE_APPLY);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑资源申请《完》")
    @PutMapping("/tasks/{id}/types/RESOURCE_APPLY")
    public void editFirstExpansion(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated ResourceTaskDTO resourceTaskDTO
    ) {
        taskService.edit(id, resourceTaskDTO, admin, TaskType.RESOURCE_APPLY);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "添加资源扩容《完》")
    @PostMapping("/tasks/types/RESOURCE_EXPANSION")
    public void addExpansion(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "资源申请表单") @RequestBody ResourceTaskDTO resourceTaskDTO
    ) {
        taskService.add(resourceTaskDTO, admin, TaskType.RESOURCE_EXPANSION);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑资源扩容《完》")
    @PutMapping("/tasks/{id}/types/RESOURCE_EXPANSION")
    public void editExpansion(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "模型任务表单") @RequestBody @Validated ResourceTaskDTO resourceTaskDTO
    ) {
        taskService.edit(id, resourceTaskDTO, admin, TaskType.RESOURCE_EXPANSION);
    }

    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "添加新数据申请《完》")
    @PostMapping("/tasks/types/NEW_DATA_APPLY")
    public void addNewDataApply(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "新数据表单") @RequestBody NewDataApplyDTO newDataApplyDTO
    ) {
        taskService.add(newDataApplyDTO, admin, TaskType.NEW_DATA_APPLY);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑新数据申请《完》")
    @PutMapping("/tasks/{id}/types/NEW_DATA_APPLY")
    public void editNewDataApply(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "新数据表单") @RequestBody @Validated NewDataApplyDTO newDataApplyDTO
    ) {
        taskService.edit(id, newDataApplyDTO, admin, TaskType.NEW_DATA_APPLY);
    }


    @AdminOperation(module = "任务操作", item = "添加")
    @ApiOperation(value = "添加设备租赁《完》")
    @PostMapping("/tasks/types/RESOURCE_DEVICE")
    public void addResourceDevice(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "设备租赁表单") @RequestBody ResourceDeviceTaskDTO resourceDeviceTaskDTO
    ) {
        taskService.add(resourceDeviceTaskDTO, admin, TaskType.RESOURCE_DEVICE);
    }

    @AdminOperation(module = "任务操作", item = "编辑")
    @ApiOperation(value = "编辑设备租赁《完》")
    @PutMapping("/tasks/{id}/types/RESOURCE_DEVICE")
    public void editResourceDevice(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "设备租赁表单") @RequestBody @Validated ResourceDeviceTaskDTO resourceDeviceTaskDTO
    ) {
        taskService.edit(id, resourceDeviceTaskDTO, admin, TaskType.RESOURCE_DEVICE);
    }

    @ApiOperation(value = "我申请的数据模型任务列表《完》")
    @GetMapping("/tasks/types/DATA_MODEL_AUTH/my")
    public PagerVO<MyDataModelAuthTask> myDataModelAuthList(
            @ApiParam(value = "任务名称") @RequestParam(required = false) String name,
            @ApiParam(value = "审核状态") @RequestParam(required = false) CheckState state,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiIgnore @LoginAdmin Admin admin
    ) {
        return taskService.myDataModelAuthList(name, state, admin, page, pageSize);
    }

    @ApiOperation(value = "通用任务详情《完》")
    @GetMapping("/tasks/{id}")
    public TaskDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiIgnore @LoginAdmin Admin admin
    ) {
        return taskService.detail(id, admin);
    }

    @ApiOperation(value = "任务编辑记录《完》")
    @GetMapping("/tasks/{id}/histories")
    public List<TaskHistorySimpleVO> editRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskHistoryService.listHistorySimple(id);
    }

    @ApiOperation(value = "任务执行记录《完》")
    @GetMapping("/tasks/{id}/execs")
    public List<TaskExecVO> taskExecRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskExecService.list(id);
    }

    @ApiOperation(value = "任务记录《完》")
    @GetMapping("/tasks/{id}/auditRecords")
    public List<TaskAuditRecordVO> checkRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return taskAuditRecordService.list(id);
    }

    @AdminOperation(module = "任务操作", item = "删除")
    @ApiOperation(value = "删除任务《完》")
    @DeleteMapping("/tasks/{id}")
    public void delete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        taskService.delete(id, admin);
    }

    @ApiOperation(value = "数据中台-数据申请次数《完》")
    @GetMapping("/tasks/WORK_DATA/applyCnt")
    public int workDataApplyCnt() {
        return taskService.findWorkDataApplyCnt();
    }

    @ApiOperation(value = "我的任务统计数《完》")
    @GetMapping("/tasks/myTaskAnalysis")
    public MyTaskAnalysis myTaskAnalysis(@ApiIgnore @LoginAdmin Admin admin) {
        return taskService.myTaskAnalysis(admin);
    }

    @ApiOperation(value = "我申请的服务器存储资源《完》")
    @GetMapping("/tasks/itemExpansionSource")
    public List<ItemExpansionSourceDTO> itemExpansionSource(@ApiIgnore @LoginAdmin Admin admin) {
        return taskService.itemExpansionSource(admin);
    }

}
